﻿//  

#region

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Practices.Composite.Logging;
using Microsoft.Practices.ServiceLocation;

#endregion

namespace pushbomb.composite.core.Infrastructure
{
    public class LogPublisher : FrameworkElement, ILogPublisher
    {
 

         private readonly DispatcherTimer _timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(1)};
        private ObservableLogger _logger;


        public LogPublisher()
        {
            _timer.Tick += TimerOnTick;

            SetValue(LogProperty, new ObservableCollection<LogEntry>());
          }

        #region Log Dependency Property

        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<LogEntry> Log
        {
            get { return (ObservableCollection<LogEntry>) GetValue(LogProperty); }
            set { SetValue(LogProperty, value); }
        }

        /// <summary>
        /// Log Dependency Property.
        /// </summary>
        public static readonly DependencyProperty LogProperty =
            DependencyProperty.Register(
                "Log",
                typeof (ObservableCollection<LogEntry>),
                typeof (LogPublisher),
                new PropertyMetadata(new PropertyChangedCallback(LogChanged)));

        private static void LogChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LogPublisher c = d as LogPublisher;
            if (c != null)
            {
                
            }
        }

        #endregion

 
        private void TimerOnTick(object sender, EventArgs args)
        {
            if (_logger != null)
            {
                  foreach (var x in _logger.PendingItems)
                  

                     Log.Add(x);
             }
        }


     

        public void AttachLogger()
        {
              _logger = ServiceLocator.Current.GetInstance<ILoggerFacade>() as ObservableLogger;
               
          

            _timer.Start();

        }
    }
}